/*
 * This file (AxRandom.java) is protected by the GNU GPL licence (v2). 
 * Please read the license.txt attached for further informations. 
 * 
 */

package artx.utils;

import java.util.Random;

/**
 * Interface to use easily random methods.
 * 
 * @author Xavier Gouchet
 * 
 */
public class AxRandom {

	/** Random generator */
	private static Random GENERATOR = new Random();

	/** Max length of generated Strings */
	protected static int MAX_STR_LENGTH = 32;

	/**
	 * Sets the seed of the Random Generator
	 * 
	 * @param s
	 *            new seed
	 */
	public static void Seed(long s) {
		GENERATOR.setSeed(s);
	}

	/**
	 * Generates a random integer.
	 * 
	 * @return a random integer between 0 and 2<sup>32</sup>
	 */
	public static int RandI() {
		return GENERATOR.nextInt();
	}

	/**
	 * Generates a random integer.
	 * 
	 * @param max
	 *            maximum value that must be returned
	 * @return a random integer between 0 and max
	 */
	public static int RandI(int max) {
		return GENERATOR.nextInt(max);
	}

	/**
	 * Generates a random integer.
	 * 
	 * @param min
	 *            minimum value that must be returned
	 * @param max
	 *            maximum value that must be returned
	 * @return a random integer between min and max
	 */
	public static int RandI(int min, int max) {
		return (GENERATOR.nextInt(max - min) + min);
	}

	/**
	 * Generates a random float.
	 * 
	 * @return a random float between 0.0f and 1.0f
	 */
	public static float RandF() {
		return GENERATOR.nextFloat();
	}

	/**
	 * Generates a random float.
	 * 
	 * @param max
	 *            maximum value that must be returned
	 * @return a random float between 0.0f and max
	 */
	public static float RandF(float max) {
		return (GENERATOR.nextFloat() * max);
	}

	/**
	 * Generates a random float.
	 * 
	 * @param min
	 *            minimum value that must be returned
	 * @param max
	 *            maximum value that must be returned
	 * @return a random float between min and max
	 */
	public static float RandF(float min, float max) {
		return ((GENERATOR.nextFloat() * (max - min)) + min);
	}

	/**
	 * Generates a random double.
	 * 
	 * @return a random double between 0.0 and 0.1
	 */
	public static double RandD() {
		return GENERATOR.nextDouble();
	}

	/**
	 * Generates a random double.
	 * 
	 * @param max
	 *            maximum value that must be returned
	 * @return a random double between 0.0 and max
	 */
	public static double RandD(double max) {
		return (GENERATOR.nextDouble() * max);
	}

	/**
	 * Generates a random double.
	 * 
	 * @param min
	 *            minimum value that must be returned
	 * @param max
	 *            maximum value that must be returned
	 * @return a random double between min and max
	 */
	public static double RandD(double min, double max) {
		return ((GENERATOR.nextDouble() * (max - min)) + min);
	}

	/**
	 * Generates a random boolean.
	 * 
	 * @return a random boolean value
	 */
	public static boolean RandB() {
		return GENERATOR.nextBoolean();
	}
	
	/**
	 * Generates a random boolean with percent value.
	 * @param percent the percentage of true returned (value should be between 0.0 and 1.0) 
	 * @return a random boolean value
	 */
	public static boolean RandB(double percent) {
		percent = Math.max(Math.min(percent, 1.0), 0.0);
		return (GENERATOR.nextDouble() <= percent);
	}
	
	/**
	 * Generates a random boolean with percent value.
	 * @param percent the percentage of true returned (value should be between 0 and 100) 
	 * @return a random boolean value
	 */
	public static boolean RandB(int percent) {
		percent = Math.max(Math.min(percent, 100), 0);
		return (GENERATOR.nextInt(100) <= percent);
	}

	/**
	 * Generates a random string.
	 * 
	 * @return a random string which length is between 1 and
	 *         {@link #MAX_STR_LENGTH}
	 */
	public static String RandS() {
		return RandS(GENERATOR.nextInt(MAX_STR_LENGTH));
	}

	/**
	 * Generates a random string.
	 * 
	 * @param length
	 *            length of the generated random String
	 * @return a random string of the specified length
	 */
	public static String RandS(int length) {
		String res = new String();

		// TODO write a random string method
		return "";
	}
}
